<?php

/*

	Copyright © 2011 Mike Bennett <mike@kereru.com>

	This file is part of Tazu-Time.

    Tazu-Time is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Tazu-Time is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tazu-Time.  If not, see <http://www.gnu.org/licenses/>.
    
*/


class file_saveas {


	var $dialog = FALSE;
	var $filter = FALSE;

	var $filtename = FALSE;
	var $path = FALSE;
	var $caption = FALSE;


	function __construct($path,$filename,$filter=FALSE,$caption='File Save') {

		$this->path = $path;
		$this->filename = $filename;
		$this->filter = $filter;
		$this->caption = $caption;

	}
	
	function get() {

		if ( $caption ) $this->caption = $caption;

		$this->dialog = new GtkFileChooserDialog(
											$this->caption, 
											null, 
											Gtk::FILE_CHOOSER_ACTION_SAVE,
											array(Gtk::STOCK_CANCEL, Gtk::RESPONSE_CANCEL, Gtk::STOCK_OK, Gtk::RESPONSE_OK), 
											null
										);

		$this->dialog->set_icon_from_file(PATH_RES.'app_icon_16.png');
		$this->dialog->set_local_only(FALSE);
		
		
//		if ( $this->filename ) $this->dialog->set_filename($this->path.'/'.$this->filename);
		if ( $this->path ) $this->dialog->set_current_folder($this->path);
		if ( $this->filename ) $this->dialog->set_current_name($this->filename);

		if ( $this->filter ) {
			$hbox = new GtkHBox();
    		$this->dialog->vbox->pack_start($hbox, 0, 0);
    		$hbox->pack_start(new GtkLabel('File type:'), 0, 0);

		    // create a combobox listing the available filter options 
			$combobox = &GtkComboBox::new_text();
			
			foreach ( $this->filter as $label => $wildcard ) {
				$combobox->append_text($label);							
			}
			
			$combobox->connect('changed', array($this,'on_change_filter'));
			$hbox->pack_start($combobox, 0, 0);
		}
		

		$this->dialog->show_all();

		$selected_file = FALSE;
		if ($this->dialog->run() == Gtk::RESPONSE_OK) {
			$selected_file = $this->dialog->get_filename(); 
		}

		$this->dialog->destroy();

		return($selected_file);

	}

	function on_change_filter($combobox) {

	    $model = $combobox->get_model(); // get the model
	    $selected_value = $model->get_value(
        $combobox->get_active_iter(), 0);// note 3

	    $filter = new GtkFileFilter(); // note 4
        $filter->add_pattern($this->filter[$selected_value]); // note 5
	    
		$this->dialog->set_filter($filter);
	
	}

}

?>